﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Client.Properties;
using OpenSoftStore.Orm.Static;

namespace OpenSoftStore.Client.UIL
{
    public partial class FormLogin : Form
    {

        private int count = 0;
        public LoginLogic LoginBll;
        public AdminLogic LgcAdmin;

        public FormLogin()
        {
            InitializeComponent();
            LgcAdmin = new AdminLogic();
            //pblLogo.BackgroundImage =;
            pctLogo.ImageLocation = "Img/SpaLogo150.jpg";
            lblSysName.Text = SystemInfo.SysName;

        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            txtPassword.Focus();
            FormBorderStyle = FormBorderStyle.None;
            LoginBll = new LoginLogic();


            var admins = LgcAdmin.GetAdminViewFast();
            cmbAdmin.DataSource = admins;
            cmbAdmin.DisplayMember = "Name";
            cmbAdmin.ValueMember = "Id";
            cmbAdmin.Text = "超级管理员";

            cmbShop.DataSource = LgcAdmin.GetShops();
            cmbShop.DisplayMember = "Name";
            cmbShop.ValueMember = "Id";

            //if (admins.Rows.Count > 0)
            //cmbAdmin.NullValue = admins.Rows[0]["Name"];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            //if (DateTime.Now.Year != 2014)
            //{
                //FormSysMessage.ShowMessage("产品授权已经过期");
                //Close();
            //}

            if (count >= 5)//首先判断登陆次数
            {
                FormSysMessage.ShowMessage("超过系统登陆最大次数");
                Close();
            }
            if (string.IsNullOrEmpty(cmbAdmin.Text))
            {
                MessageBox.Show(Resources.FormLogin_BtnLogin_NameNull);
                return;
            }
            var data = LoginBll.Login(cmbAdmin.Text, txtPassword.Text);
            if (data)//判断是否有查询到对象的数据
            {
                if (File.Exists(@"login.txt"))//判断文件存在
                {
                    if (!cmbAdmin.AutoCompleteCustomSource.Contains(cmbAdmin.Text))//判断记录是否存在
                    {
                        var sw = new StreamWriter(@"login.txt", true);//true参数不可少，否则会覆盖以前存入的记录
                        sw.WriteLine(cmbAdmin.Text.Trim());//存入记录
                        sw.Close();//关闭文件流
                        if (!cmbAdmin.AutoCompleteCustomSource.Contains(cmbAdmin.Text))
                        {
                            cmbAdmin.AutoCompleteCustomSource.Add(cmbAdmin.Text);
                        }
                    }
                }
                DialogResult = DialogResult.OK;
            }
            else
            {
                FormSysMessage.ShowMessage("用户名或密码错误");
                txtPassword.Focus();
                count++;
            }
        }

        private void btnExt_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter) BtnSave_Click(null,null);
        }
    }
}
